document.addEventListener("DOMContentLoaded", function () {
  const input = document.getElementById("input");
  const output = document.getElementById("output");

  // 转大写
  document.getElementById("uppercase").addEventListener("click", function () {
    output.value = input.value.toUpperCase();
  });

  // 转小写
  document.getElementById("lowercase").addEventListener("click", function () {
    output.value = input.value.toLowerCase();
  });

  // 首字母大写
  document.getElementById("capitalize").addEventListener("click", function () {
    output.value = input.value
      .toLowerCase()
      .split(" ")
      .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
      .join(" ");
  });

  // 首字母小写
  document
    .getElementById("decapitalize")
    .addEventListener("click", function () {
      output.value = input.value
        .split(" ")
        .map((word) => {
          if (word.length <= 1) return word.toLowerCase();
          return word.charAt(0).toLowerCase() + word.slice(1).toUpperCase();
        })
        .join(" ");
    });

  // 空格转下划线
  document
    .getElementById("space-to-line")
    .addEventListener("click", function () {
      output.value = input.value.replace(/\s+/g, "_");
    });

  // 下划线转空格
  document
    .getElementById("line-to-space")
    .addEventListener("click", function () {
      output.value = input.value.replace(/_+/g, " ");
    });

  // 清除空格
  document.getElementById("clear-input").addEventListener("click", function () {
    output.value = input.value.replace(/\s+/g, "");
  });

  // 清除换行
  document.getElementById("clear-line").addEventListener("click", function () {
    output.value = input.value.replace(/[\r\n]+/g, "");
  });

  // 复制结果
  document.getElementById("copy").addEventListener("click", async function () {
    try {
      await navigator.clipboard.writeText(output.value);
      // 可选：添加复制成功的提示
      this.textContent = "已复制!";
      setTimeout(() => {
        this.textContent = "复制结果";
      }, 1000);
    } catch (err) {
      console.error("复制失败:", err);
    }
  });

  // 下划线→驼峰
  document
    .getElementById("underline-to-camel")
    .addEventListener("click", function () {
      output.value = input.value
        .split("_")
        .map((word, index) => {
          if (index === 0) return word.toLowerCase();
          return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
        })
        .join("");
    });

  // 空格→驼峰
  document
    .getElementById("space-to-camel")
    .addEventListener("click", function () {
      output.value = input.value
        .split(" ")
        .map((word, index) => {
          if (index === 0) return word.toLowerCase();
          return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
        })
        .join("");
    });

  // 驼峰→下划线
  document
    .getElementById("camel-to-underline")
    .addEventListener("click", function () {
      output.value = input.value
        .replace(/([A-Z])/g, "_$1")
        // .toLowerCase()
        .replace(/^_/, "");
    });

  // 驼峰→空格
  document
    .getElementById("camel-to-space")
    .addEventListener("click", function () {
      output.value = input.value
        .replace(/([A-Z])/g, " $1")
        // .toLowerCase()
        .replace(/^ /, "");
    });

  // 空格→中横线
  document
    .getElementById("space-to-dash")
    .addEventListener("click", function () {
      output.value = input.value.replace(/\s+/g, "-");
    });
  // 中横线→空格
  document
    .getElementById("dash-to-space")
    .addEventListener("click", function () {
      output.value = input.value.replace(/[-]/g, " ");
    });

  // 下划线→中横线
  // document
  //   .getElementById("underline-to-dash")
  //   .addEventListener("click", function () {
  //     output.value = input.value.replace(/_+/g, "-");
  //   });

  // 中横线→下划线
  // document
  //   .getElementById("dash-to-underline")
  //   .addEventListener("click", function () {
  //     output.value = input.value.replace(/-+/g, "_");
  //   });

  // 下划线→小数点
  document
    .getElementById("underline-to-dot")
    .addEventListener("click", function () {
      output.value = input.value.replace(/_+/g, ".");
    });

  // 小数点→下划线
  document
    .getElementById("dot-to-underline")
    .addEventListener("click", function () {
      output.value = input.value.replace(/\.+/g, "_");
    });

  // 清空按钮
  document.getElementById("clear").addEventListener("click", function () {
    input.value = "";
    output.value = "";
  });
});
